package edu.xmut.yuepai.user.contoller;

import cn.hutool.core.map.MapUtil;
import edu.xmut.yuepai.common.result.R;
import edu.xmut.yuepai.model.form.UserLoginForm;
import edu.xmut.yuepai.user.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

@RestController
public class LoginController {

    @Resource
    private UserService userService;

    @GetMapping("/login/code/{phone}")
    public R getCode(@PathVariable("phone") String phone) {
        String code = userService.getCode(phone);
        return R.ok(code);
    }

    @GetMapping("/login/{phone}/{code}")
    public R loginByCode(
            @PathVariable("phone") String phone,
            @PathVariable("code") String code) {
        String token = userService.loginByCode(phone, code);
        HashMap<String, String> data = new HashMap<>();
        data.put("token", token);
        return R.ok(data);
    }

    @PostMapping("/login")
    public R login(@RequestBody UserLoginForm userLoginForm) {
        String token = userService.loginByPassword(userLoginForm);
        HashMap<String, String> data = MapUtil.of("token", token);
        return R.ok(data);
    }


}
